<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue'

const { proxy } = getCurrentInstance()

const user = reactive(JSON.parse(sessionStorage.getItem('CurUser')))
const storageData = ref([])
const goodstypeData = ref([])
const tableData = ref([])
const pageSize = ref(10)
const pageNum = ref(1)
const total = ref(0)
const name = ref('')
const storage = ref('')
const goodstype = ref('')
const centerDialogVisible = ref(false)
const formRef = ref(null)
const form = reactive({
    id: '',
    name: '',
    storage: '',
    goodstype: '',
    count: '',
    remark: ''
})

function formatStorage(row) {
    const temp = storageData.value.find(item => item.id == row.storage)
    return temp && temp.name
}
function formatGoodstype(row) {
    const temp = goodstypeData.value.find(item => item.id == row.goodstype)
    return temp && temp.name
}
function resetForm() {
    formRef.value?.resetFields()
}
function handleSizeChange(val) {
    pageNum.value = 1
    pageSize.value = val
    loadPost()
}
function handleCurrentChange(val) {
    pageNum.value = val
    loadPost()
}
function resetParam() {
    name.value = ''
    storage.value = ''
    goodstype.value = ''
}
function loadStorage() {
    proxy.$axios.get(proxy.$httpUrl + '/storage/list').then(res => res.data).then(res => {
        if (res.code == 200) {
            storageData.value = res.data
        } else {
            alert('获取数据失败')
        }
    })
}
function loadGoodstype() {
    proxy.$axios.get(proxy.$httpUrl + '/goodstype/list').then(res => res.data).then(res => {
        if (res.code == 200) {
            goodstypeData.value = res.data
        } else {
            alert('获取数据失败')
        }
    })
}
function loadPost() {
    proxy.$axios.post(proxy.$httpUrl + '/record/listPage', {
        pageSize: pageSize.value,
        pageNum: pageNum.value,
        param: {
            name: name.value,
            goodstype: goodstype.value + '',
            storage: storage.value + '',
            roleId: user.roleId + '',
            userId: user.id + ''
        }
    }).then(res => res.data).then(res => {
        if (res.code == 200) {
            tableData.value = res.data
            total.value = res.total
        } else {
            alert('获取数据失败')
        }
    })
}

onMounted(() => {
    loadStorage()
    loadGoodstype()
    loadPost()
})
</script>

<template>
    <div>
        <div style="margin-bottom: 5px;">
            <el-input v-model="name" placeholder="请输入物品名" suffix-icon="el-icon-search" style="width: 200px;"
                @keyup.enter.native="loadPost"></el-input>
            <el-select v-model="storage" placeholder="请选择仓库" style="margin-left: 5px;">
                <el-option v-for="item in storageData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
            <el-select v-model="goodstype" placeholder="请选择分类" style="margin-left: 5px;">
                <el-option v-for="item in goodstypeData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>

            <el-button type="primary" style="margin-left: 5px;" @click="loadPost">查询</el-button>
            <el-button type="success" @click="resetParam">重置</el-button>


        </div>
        <el-table :data="tableData" :header-cell-style="{ background: '#f2f5fc', color: '#555555' }" border>
            <el-table-column prop="id" label="ID" width="60">
            </el-table-column>
            <el-table-column prop="goodsname" label="物品名" width="180">
            </el-table-column>
            <el-table-column prop="storagename" label="仓库" width="180">
            </el-table-column>
            <el-table-column prop="goodstypename" label="分类" width="180">
            </el-table-column>
            <el-table-column prop="adminname" label="操作人" width="180">
            </el-table-column>
            <el-table-column prop="username" label="申请人" width="180">
            </el-table-column>
            <el-table-column prop="count" label="数量" width="180">
            </el-table-column>
            <el-table-column prop="createtime" label="操作时间" width="180">
            </el-table-column>
            <el-table-column prop="remark" label="备注">
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
            :page-sizes="[5, 10, 20, 30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
</template>



<style scoped></style>